<template>
	<Layout>
		<template #title>关注</template>

		<view class="list-wrap">
			<view v-for="item of list" :key="item.followId" class="list-item" @click.stop="tapToDetail(item)">
				<image class="list-item__portrait" :src="item.avatar"></image>
				<view class="list-item__law">
					<view class="list-item__law__name">
						<view class="ellipsis">{{item.followeeName}}</view>
					</view>
					<view class="list-item__law__intro">
						<view class="ellipsis">{{item.followeeDesc}}</view>
					</view>
				</view>
				<view class="list-item__follow" @click.stop="tapUnfollow(item.followeeId)">已关注</view>
			</view>
		</view>
		<LoginPopup @success="loadData"></LoginPopup>
	</Layout>
</template>

<script lang="ts" setup name="MineFocus">
	import { ref } from 'vue';
	import { onLoad, onPullDownRefresh } from '@dcloudio/uni-app';
	import type { AnyObj } from '@/types/global';
	import { getUserFollowList, saveUnfollow } from '@/apis/auth';

	const list = ref<AnyObj[]>([]);

	function loadData() {
		getUserFollowList().then((res) => {
			list.value = res.rows ?? [];
		});
	}

	function tapToDetail(item : any) {
		const url = `/pages-lawyer/law${item.followeeScope == '1' ? 'yer' : ''}-detail/law${item.followeeScope == '1' ? 'yer' : ''}-detail?lawFirmId=${item.followeeId}`;
		uni.navigateTo({
			url
		});
	}

	function tapUnfollow(followeeId : string) {
		saveUnfollow(followeeId).then((res) => {
			if (res.code !== 200) {
				uni.showToast({
					icon: 'none',
					title: res.msg ?? '服务器忙，请稍后再试'
				});
				return;
			}
			uni.showToast({
				icon: 'none',
				title: '已取消'
			});
			setTimeout(() => {
				loadData();
			}, 1500);
		});
	}

	onPullDownRefresh(() => {
		loadData();
		setTimeout(() => {
			uni.stopPullDownRefresh();
		}, 500);
	});

	onLoad(() => {
		loadData();
	});
</script>

<style lang="scss" scoped>
	.list-wrap {
		width: 100vw;
		height: 100%;
		overflow-y: auto;
		margin-top: 30rpx;
		padding: 14rpx 32rpx 44rpx 32rpx;
		box-sizing: border-box;
		background: #FFFFFF;
		border-radius: 36rpx 36rpx 0rpx 0rpx;

		.list-item {
			display: inline-flex;
			justify-content: space-between;
			align-items: center;
			flex-shrink: 0;
			width: 100%;
			height: 170rpx;

			&+.list-item {
				border-top: 2rpx solid #E6E5E5;
			}

			&__portrait {
				display: inline-flex;
				flex-shrink: 0;
				width: 92rpx;
				height: 92rpx;
				border-radius: 50%;
			}

			&__law {
				display: inline-flex;
				flex-shrink: 0;
				flex-direction: column;
				justify-content: space-between;
				width: 440rpx;
				height: 92rpx;

				&__name {
					display: inline-flex;
					width: 100%;
					line-height: 38rpx;
					font-weight: 500;
					font-size: 32rpx;
					color: #101010;
				}

				&__intro {
					display: inline-flex;
					width: 100%;
					line-height: 33rpx;
					font-weight: 400;
					font-size: 28rpx;
					color: #929292;
				}
			}

			&__follow {
				display: inline-flex;
				justify-content: center;
				align-items: center;
				flex-shrink: 0;
				width: 124rpx;
				height: 48rpx;
				background: #F3F3F3;
				border-radius: 46rpx;
				font-weight: 400;
				font-size: 24rpx;
				color: #929292;
			}
		}
	}
</style>